package com.itheima.infoManager.duotaiDemo.manager.controller;

import com.itheima.infoManager.duotaiDemo.manager.domain.Teacher;
import com.itheima.infoManager.duotaiDemo.manager.service.TeacherService;

import java.util.Scanner;

public  abstract class BaseTeacherController {
    private Scanner sc = new Scanner(System.in);

    private TeacherService service = new TeacherService();
    //教师菜单
    public  void strart(){
        stop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            switch (sc.nextInt()){
                case 1:
                    //添加
                    addTeacher();
                    break;
                case 2:
                    //删除
                    deleteTeacher();
                    break;
                case 3:
                    //修改
                    updateTeacher();
                    break;
                case 4:
                    //查看
                    findAllTeacher();
                    break;
                case 5:
                    //退出
                    break stop;
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }
    //修改
    public final void updateTeacher() {
        //判断这个ID是否在数组中
        String upId = judge();
        Teacher teacher = inputTeacher(upId);
        boolean flag = service.updateTeacher(upId,teacher);
        if (flag) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }

    //删除
    public final void deleteTeacher() {
        //判断这个ID是否在数组中
        String delId = judge();
        //调用Service中的方法删除
        boolean flag = service.deleteTeacher(delId);
        if (flag) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }

    }

    //查看
    public final void findAllTeacher() {
        //返回null或者数组
        Teacher[] teachers = service.findAllTeacher();
        if (teachers==null){
            System.out.println("无数据，请先添加学生！");
            return;
        }

        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher!=null){
                System.out.println(teacher.getId()+"\t"+teacher.getName()+"\t"+teacher.getAge()+"\t\t"+teacher.getBirthday());
            }
        }
    }

    //添加
    public final void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师id：");
            id = sc.next();
            if (!service.isExists(id)){//判断当前输入的id是否和数组中的数据有重复
                break;
            }
            System.out.println("工号重复,请重新输入：");
        }
        //输入姓名、年龄和生日的方法
        Teacher teacher = inputTeacher(id);
        boolean flag = service.addTeacher(teacher);//将封装好的学生对象交给service层去完成添加操作
        if (flag) {//根据返回值提示添加成功或失败
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    //封装
    public abstract Teacher inputTeacher(String id);

    //判断
    public String judge(){
        String id;
        while (true) {
            System.out.println("输入老师id：");
            id = sc.next();
            //判断：id是否存在，存在的话在哪个位置？
            if (!service.isExists(id)) {
                System.out.println("查无此id，请重新输入");
            }else {
                break;
            }
        }
        return id;
    }


}
